#!/bin/sh

########################################################################################
#
#  postinstall script for winclone to create partition based on size and then
#  restore a winclone image on it.  All the settings are passed in via the
#  $1/WinclonePayload/settings.sh.  settings.sh should contain these items:
#
#   $mode: either "create" or "existing".  If create, a bootcamp partition will
#          be created.  This requires a size (either bytes or percent).  "existing"
#          will check to make sure the partition exists, is NTFS or MSDOS, and will
#          then use that to restore the Winclone image onto. requires either
#          $size or $percent to be set, as well as $mac_device.
#
#   $slice: if mode is "existing", this must be set to the slice on the select volume to
#           restore to.
#
#    $mac_device: If mode is "create", mac_device is the path to the mac volume
#                 that will be resized to create the new windows partition
#
#    $size: size in kbytes of the new Windows partition in "create" mode.
#
#    $percent: size in percent of the new Windows partition in "create" mode.
#
#
########################################################################################

echo "Starting Post Install Script"

if [ ! -f "$1/Contents/Resources/settings.sh" ] ; then
    echo "Could not find setting file at $1/Contents/Resources/settings.sh.  Quitting"
    exit -1

fi
source "$1/Contents/Resources/settings.sh"

echo "Creating bootcamp partition section"
echo "Checking out the partition table on device $3"

if [ -z "${mode}" ] ; then

    echo "Could not find ${mode}.  Exiting"
    exit -1
fi


if [ $mode = "create" ] ; then
    echo "Mode: Create Partition"


    echo "    checking size..."

    partition_size=`diskutil info -plist "/" |grep -A 1 TotalSize|tail -1|sed 's|.*<integer>\(.*\)</integer>.*|\1|'`

    if [ $partition_size -gt "2418925581107" ]; then

        echo "  partition to split is larger than 2.2 TB.  Exiting."
        exit -1
    fi

    echo "    checking for existing bootcamp partitions..."
    existing_bc_partitions=`diskutil list|grep 'Microsoft Basic Data'|wc -l`

    if [ $existing_bc_partitions -gt 0 ] ; then
        echo "  Bootcamp partition already exists.  Exiting"

        exit -1
    fi

    echo "    checking for core storage..."

    is_core_storage=`diskutil cs info "$3"`

    if [ $? -eq 0 ] ; then
        echo "Selected volume is core storage, which is not currently supported"
        exit -1
    fi

    echo "  Making sure that the partition to split is JHFS+"

    fs_type=`diskutil info -plist "$3" |grep -A 1 FilesystemName|tail -1|sed 's|.*<string>\(.*\)</string>.*|\1|'`

    if [ -z "${fs_type}" ] ; then

        echo "  Could not find $3.  Exiting."
        exit -1
    fi

    if [ "${fs_type}" = "Journaled HFS+" ] ; then

        echo "  Filesystem is $fs_type so we are good to try and split it..."

        if [[ -n "$size" ]] ; then

            echo "Creating by size"

            hfs_size=`diskutil info -plist $3 |grep -A 1 TotalSize|tail -1|sed 's|.*<integer>\(.*\)</integer>.*|\1|'`
            new_hfs_size="$(($hfs_size-$size))b"
            echo hfs_size is $hfs_size
            echo new_hfs_size is $new_hfs_size

        elif [[ -n "$percent" ]] ; then

            new_hfs_size=$percent

        else

            echo "  You must specify either size or percent.  Exiting."
            exit -1
        fi


        if [ -z "$3" ] ; then

            echo "  Could not find $3.  Quitting"
            exit -1

        fi

        if [ -z "$new_hfs_size" ] ; then

            echo "  Could not find ${new_hfs_size}.  Quitting"
            exit -1

        fi


        restore_device=`diskutil resizeVolume "$3" "${new_hfs_size}" MS-DOS DOS 0b|grep "Microsoft Basic Data"|sed 's/.*\(disk.*s.*\)/\1/'`

        if [ -z "$restore_device" ] ; then

            echo "  Could not find ${restore_device}.  Quitting"
            exit -1

        fi

        restore_device="/dev/${restore_device}"

        echo "  Completed resizing.  New device is ${restore_device}"
    else
        echo "Specified partition $3  is not Journaled HFS+"
        return -1

    fi
elif [ $mode = "existing" ]; then

    if [ -z "$slice" ] ; then

        echo "  Could not find ${slice}.  Quitting"
        exit -1

    fi

    echo "  Using slice ${slice}"

    wholedisk=`diskutil info -plist "$3" |grep -A 1 ParentWholeDisk|tail -1|sed 's|.*<string>\(.*\)</string>.*|\1|'`


    restore_device="/dev/${wholedisk}s${slice}"
    echo restoring to device $restore_device
fi


echo "Restore to existing partition mode....  Progress log is at /tmp/winclone_package.log"
echo "  checking to see if partition ${restore_device} exists"

fs_type=`diskutil info -plist ${restore_device} | grep -A 1 FilesystemType |tail -1|sed 's|.*<string>\(.*\)</string>.*|\1|'`

if [ -z "${fs_type}" ] ; then

    echo "  could not find device ${restore_device}"
    exit -1

fi

if [ $fs_type = "ntfs" ] || [ $fs_type = "msdos" ] || [ $fs_type = "exfat" ] ; then

    echo "  Filesystem is $fs_type so we are good to restore to it..."

    cd "$1"/Contents/Resources/*.winclone/

    ./winclone_helper_tool --self-extract -p ${restore_device} 2>&1 >>/tmp/winclone_package.log

    if [ $? -ne 0 ] ; then
        echo "Failed restoring to ${restore_device}."
        exit -1
    fi
else
   
    echo "   Filesystem is not ntfs, msdos or exfat!  Exiting."
    exit -1

fi



exit 0
